package com.entrogy.service.impl;

import com.entrogy.bean.*;
import com.entrogy.dao.*;
import com.entrogy.service.MaintenanceService;
import com.entrogy.utils.constants.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @ClassName MaintenanceServiceImpl
 * @Description
 * @Author luoxingjiang
 * @Date 2019-12-11 上午 11:22
 * @Version 1.0
 **/
@Service
public class MaintenanceServiceImpl implements MaintenanceService {

    @Autowired
    private MaintenanceMapper maintenanceMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ParentMapper parentMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private MachineMapper machineMapper;


    @Override
    public MaintenanceUseOverViewEntity getUseInfo(Integer schoolCode, Date startDate, Date endDate) {
        MaintenanceUseOverViewEntity schoolUseInfo = new MaintenanceUseOverViewEntity();
        // 定义学校使用情况的字段
        List<MaintenanceMessageUseEntity> messageUseList = new ArrayList<>();
        Integer messageUseTotalCount = 0;
        Integer shortMessageSendTotalCount = 0;
        Integer messageReceivedTotalCount = 0;
        List<MaintenanceHomeworkUseEntity> homeworkUseList = new ArrayList<>();
        Integer homeworkUseTotalCount = 0;
        Integer homeworkReceivedTotalCount = 0;
        List<MaintenanceScoreSendUseEntity> scoreSendUseList = new ArrayList<>();
        Integer scoreSendUseTotalCount = 0;
        Integer scoreReceivedTotalCount = 0;
        List<MaintenanceAskForLeaveEntity> askForLeaveUseList = new ArrayList<>();
        Integer askForLeaveTotalCount = 0;
        List<MaintenanceApplyVisitUseEntity> applyVisitUseList = new ArrayList<>();
        Integer applyVisitTotalCount = 0;
        Double betweenDays = null;
        if (startDate != null && endDate != null){
            betweenDays = Double.valueOf((endDate.getTime() - startDate.getTime()) / (1000 * 3600 * 24)) + 1;
        }
        if (betweenDays != null){
            while (endDate.after(startDate)){
                MaintenanceMessageUseEntity messageUse = new MaintenanceMessageUseEntity();
                messageUse.setAnalysisDate(startDate);
                MaintenanceHomeworkUseEntity homeworkUse = new MaintenanceHomeworkUseEntity();
                homeworkUse.setAnalysisDate(startDate);
                MaintenanceAskForLeaveEntity askForLeaveUse = new MaintenanceAskForLeaveEntity();
                askForLeaveUse.setAnalysisDate(startDate);
                MaintenanceApplyVisitUseEntity applyVisitUse = new MaintenanceApplyVisitUseEntity();
                applyVisitUse.setAnalysisDate(startDate);
                MaintenanceScoreSendUseEntity scoreSendUse = new MaintenanceScoreSendUseEntity();
                scoreSendUse.setAnalysisDate(startDate);

                Date selectStartDate = startDate;
                // 天数增加一天
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(startDate);
                calendar.add(Calendar.DATE, 1);
                startDate = calendar.getTime();
                setTimeFrame(selectStartDate, startDate);

                // 查询学校发送信息使用情况（按天统计）
                // 消息发送统计
                Integer messageSendCount = maintenanceMapper.countMessageSend(schoolCode, selectStartDate, startDate);
                messageUseTotalCount += messageSendCount;
                // 短信发送统计
                Integer shortMessageSendCount = maintenanceMapper.countShortMessageSend(schoolCode, selectStartDate, startDate);
                shortMessageSendTotalCount += shortMessageSendCount;
                // 消息接收统计
                Integer messageReceivedCount = maintenanceMapper.countReceivedMessage(schoolCode, selectStartDate, startDate);
                messageReceivedTotalCount += messageReceivedCount;
                messageUse.setSendCount(messageSendCount).setReceivedCount(messageReceivedCount).setShortMessageSendCount(shortMessageSendCount);
                messageUseList.add(messageUse);

                // 查询学校发送作业使用情况
                // 作业发送统计
                Integer homeworkSendCount = maintenanceMapper.countHomeworkSend(schoolCode, selectStartDate, startDate);
                homeworkUseTotalCount += homeworkSendCount;
                // 作业接收统计
                Integer homeworkReceivedCount = maintenanceMapper.countHomeworkReceived(schoolCode, selectStartDate, startDate);
                homeworkReceivedTotalCount += homeworkReceivedCount;
                homeworkUse.setSendCount(homeworkSendCount).setReceivedCount(homeworkReceivedCount);
                homeworkUseList.add(homeworkUse);

                // 查询学校成绩发送使用情况
//                Integer scoreSendCount = maintenanceMapper.countScoreSend();
//                Integer scoreReceivedCount = maintenanceMapper.countScoreReceived();
//                scoreSendUse.setSendCount(scoreSendCount).setReceivedCount(scoreReceivedCount).setFrequency(betweenDays == null ? null : scoreSendCount / betweenDays);
                scoreSendUseList.add(scoreSendUse);
                // 查询学校请假功能使用情况
                Integer askForLeaveCount = maintenanceMapper.countAskForLeaveUse(schoolCode, selectStartDate, startDate);
                askForLeaveTotalCount += askForLeaveCount;
                askForLeaveUse.setAskForLeaveCount(askForLeaveCount);
                askForLeaveUseList.add(askForLeaveUse);
                // 查询学校进校申请使用情况
                Integer applyVisitCount = maintenanceMapper.countApplyVisitUse(schoolCode, selectStartDate, startDate);
                applyVisitTotalCount += applyVisitCount;
                applyVisitUse.setApplyVisitCount(applyVisitCount);
                applyVisitUseList.add(applyVisitUse);
            }
        }
        schoolUseInfo.setMessageUse(messageUseList).setMessageUseFrequency(new BigDecimal(messageUseTotalCount / betweenDays).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue())
                .setHomeworkUse(homeworkUseList).setHomeworkUseFrequency(new BigDecimal(homeworkUseTotalCount / betweenDays).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue())
                .setAskForLeaveUse(askForLeaveUseList).setAskForLeaveUseFrequency(new BigDecimal(askForLeaveTotalCount / betweenDays).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue())
                .setApplyVisitUse(applyVisitUseList).setApplyVisitUseFrequency(new BigDecimal(applyVisitTotalCount / betweenDays).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue())
                .setScoreSendUse(scoreSendUseList).setScoreSendUseFrequency(new BigDecimal(scoreSendUseTotalCount / betweenDays).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        return schoolUseInfo;
    }

    @Override
    public MaintenanceFocusOverViewEntity getFocusInfo(Integer schoolCode, Date startDate, Date endDate) {
        // 关注数概览
        MaintenanceFocusOverViewEntity focusInfo = new MaintenanceFocusOverViewEntity();
        setTimeFrame(startDate, endDate);
        // 查询每天的关注人数
        List<MaintenanceFocusEntity> teacherFocusInfoList = maintenanceMapper.listTeacherFocusInfo(schoolCode, startDate, endDate);
        List<MaintenanceFocusEntity> parentFocusInfoList = maintenanceMapper.listParentFocusInfo(schoolCode, startDate, endDate);
        Integer teacherFocusTotalCount = 0;
        Integer parentFocusTotalCount = 0;
        for(MaintenanceFocusEntity teacherFocusInfo : teacherFocusInfoList){
            teacherFocusTotalCount += teacherFocusInfo.getFocusCount() == null ? 0 : teacherFocusInfo.getFocusCount();
        }
        for (MaintenanceFocusEntity parentFocusInfo : parentFocusInfoList) {
            parentFocusTotalCount += parentFocusInfo.getFocusCount() == null ? 0 : parentFocusInfo.getFocusCount();
        }
        focusInfo.setParentFocusInfoList(parentFocusInfoList).setParentFocusTotalCount(parentFocusTotalCount)
                .setTeacherFocusInfoList(teacherFocusInfoList).setTeacherFocusTotalCount(teacherFocusTotalCount);
        return focusInfo;
    }

    @Override
    public MaintenanceLoginOverViewEntity getLoginInfo(Integer schoolCode, Date startDate, Date endDate) {
        MaintenanceLoginOverViewEntity loginInfo = new MaintenanceLoginOverViewEntity();
        setTimeFrame(startDate, endDate);
        List<MaintenanceLoginEntity> teacherLoginInfoList = maintenanceMapper.listTeacherLoginInfo(schoolCode, startDate, endDate);
        List<MaintenanceLoginEntity> parentLoginInfoList = maintenanceMapper.listParentLoginInfo(schoolCode, startDate, endDate);
        Integer teacherLoginTotalCount = 0;
        Integer parentLoginTotalCount = 0;
        for(MaintenanceLoginEntity teacherLoginInfo : teacherLoginInfoList){
            teacherLoginTotalCount += teacherLoginInfo.getLoginCount() == null ? 0 : teacherLoginInfo.getLoginCount();
        }
        for (MaintenanceLoginEntity parentLoginInfo : parentLoginInfoList) {
            parentLoginTotalCount += parentLoginInfo.getLoginCount() == null ? 0 : parentLoginInfo.getLoginCount();
        }
        loginInfo.setParentLoginInfoList(parentLoginInfoList).setParentLoginTotalCount(parentLoginTotalCount)
                .setTeacherLoginInfoList(teacherLoginInfoList).setTeacherLoginTotalCount(teacherLoginTotalCount);
        return loginInfo;
    }

    @Override
    public MaintenanceRegisterOverViewEntity getRegisterInfo(Integer schoolCode, Date startDate, Date endDate) {
        MaintenanceRegisterOverViewEntity registerInfo = new MaintenanceRegisterOverViewEntity();
        setTimeFrame(startDate, endDate);
        List<MaintenanceRegisterEntity> teacherRegisterInfoList = maintenanceMapper.listTeacherRegisterInfo(schoolCode, startDate, endDate);
        List<MaintenanceRegisterEntity> parentRegisterInfoList = maintenanceMapper.listParentRegisterInfo(schoolCode, startDate, endDate);
        Integer teacherRegisterTotalCount = 0;
        Integer parentRegisterTotalCount = 0;
        for(MaintenanceRegisterEntity teacherRegisterInfo : teacherRegisterInfoList){
            teacherRegisterTotalCount += teacherRegisterInfo.getRegisterCount() == null ? 0 : teacherRegisterInfo.getRegisterCount();
        }
        for (MaintenanceRegisterEntity parentRegisterInfo : parentRegisterInfoList) {
            parentRegisterTotalCount += parentRegisterInfo.getRegisterCount() == null ? 0 : parentRegisterInfo.getRegisterCount();
        }
        registerInfo.setParentRegisterInfoList(parentRegisterInfoList).setParentRegisterTotalCount(parentRegisterTotalCount)
                .setTeacherRegisterInfoList(teacherRegisterInfoList).setTeacherRegisterTotalCount(teacherRegisterTotalCount);
        return registerInfo;
    }

    @Override
    public List<MaintenanceParentEntity> listParent(Integer schoolCode, String parentPhone, String parentName) {
        List<MaintenanceParentEntity> parentList = maintenanceMapper.listParent(schoolCode, parentPhone, parentName);
        return parentList;
    }

    @Override
    public List<MaintenanceMessageEntity> listSchoolMessage(Integer schoolCode) {
        return maintenanceMapper.listSchoolMessage(schoolCode);
    }

    @Override
    public MaintenanceSchoolEntity getSchoolInfo(Integer schoolCode) {
        MaintenanceSchoolEntity schoolInfo = maintenanceMapper.getSchoolInfo(schoolCode);
        int parentLoginCount = parentMapper.countParentLogin(schoolCode, null, null);
        schoolInfo.setTeacherCount(userMapper.countUser(schoolCode, null))
                .setParentCount(parentMapper.countSchoolParent(schoolCode, null, null))
                .setStudentCount(studentMapper.countStudent(schoolCode, null, null, null))
                .setParentLoginCount(parentLoginCount);
        // 人脸识别设备=大门设备+会议室设备+宿舍设备
        MachineEntity machine = new MachineEntity();
        machine.setSchoolCode(schoolCode).setDeviceModel(Constants.THDID_AI06);
        List<String> doorMachineIdList = machineMapper.listMachineId(machine);
        Integer doorMachineCount = doorMachineIdList.size();
        // 闸机数量（等于大门的平板数量/2）
        Integer gateMachineCount = doorMachineCount / 2;
        Integer meetingRoomMachineCount = machineMapper.countMeetingRoomMachine(schoolCode);
        Integer dormMachineCount = machineMapper.countDormMachine(schoolCode);
        Double parentLoginRate = maintenanceMapper.getParentLoginRate(schoolCode);
        schoolInfo.setGateMachineCount(gateMachineCount)
                .setMeetingRoomMachineCount(meetingRoomMachineCount)
                .setDormMachineCount(dormMachineCount)
                .setDoorMachineCount(doorMachineCount)
                .setParentLoginRate(new BigDecimal(parentLoginRate).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        return schoolInfo;
    }

    private void setTimeFrame(Date startDate, Date endDate){
        startDate.setHours(0);
        startDate.setMinutes(0);
        startDate.setSeconds(0);
        endDate.setHours(23);
        endDate.setMinutes(59);
        endDate.setSeconds(59);
    }


}

